<?php
// +----------------------------------------------------------------------
// | Scallion 
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.scallion.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liuxg < 420695648@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
class CommonController extends HomeBaseController
{
	public function bind_wx($param){
		if($param){
			if(isset($param['id'])){
				$user = db('user')->where(['id'=>$param['id']])->find();
				if($user){
					if($user['user_status']==1){
						$wx = new \wechatcontroller;
						$fans = $wx->wx_get_fans($param['openid']);
						$avatar['avatar'] = $fans['headimgurl'];
						$ver1 = db('user')->where('id='.$user['id'])->field('avatar')->update($avatar);
						if($is_bind_user = db('third_party_user')->where(['user_id'=>$user['id']])->find()){
							// $this->error('该用户已绑定微信，请解绑后操作！',url('portal/index/index'));
							if($is_bind_user['openid']==$fans['openid']){
								return -9;
							}
							else{
								return -1;
							}
						}
						elseif($is_bind_user = db('third_party_user')->where(['openid'=>$fans['openid']])->find()){
							// $this->error('该微信已绑定其他用户'.$is_bind_user['nickname'].',请解绑后操作！',url('portal/index/index'));
							return -2;
						}
						else{
							$data['user_id'] = $user['id'];
							$data['create_time'] = time();
							$data['status'] = 1;
							$data['nickname'] = $fans['nickname'];
							$data['openid'] = $fans['openid'];
							$ver2 = db('third_party_user')->insert($data);
							if($ver2){
								$jobHandlerClassName  = 'app\portal\controller\send_message'; 
								$jobQueueName  	      = "wx_bind"; 
								$jobData       	      = [ 'openid' => $fans['openid'],'user_nickname'=>$user['user_nickname'],'mobile'=>$user['mobile']];
								\think\Queue::push($jobHandlerClassName,$jobData,$jobQueueName);	
								  
								// $this->success('绑定成功！',url('portal/index/index'));
								return 1;
							}
							else{
								// $this->error('绑定失败！',url('portal/index/index'));
								return -3;
							}
						}
					}
					else{
						// $this->error('用户已被封禁！',url('portal/index/index'));
						return -4;
					}
				}
				else{
					// $this->error('用户不存在',url('portal/index/index'));
					return -5;
				}
			}
			else{
				// $this->error('非法访问！',url('portal/index/index'));
				return -6;
			}
		}
		else{
			// $this->error('非法访问！',url('portal/index/index'));
			return -7;
		}
	}
	
	public function is_bind_wx(){
		$param = $this->request->param();
		$is_bind_user = db('third_party_user')->where(['user_id'=>$param['id']])->find();
		if($is_bind_user){
			return true;
		}
		else{
			return false;
		}
	}

	public function wx_cancel_bind()
    {
        $id = input('param.id', 0, 'intval');
        if (empty($id)) {
            $this->error('非法数据！');
        }
        db("third_party_user")->where("id", $id)->delete();
        $this->success("解绑成功！");
    }
}